﻿using System;
using System.Collections.Generic;
using System.Linq;
using PeopleException;
using Interfaces;
using PeopleEntity;

namespace PeopleDal
{
    public class Peoples : IPeoplesDal
    {
        private readonly PeopleContext _context;
        public Peoples()
        {
           //_context = new PeopleContext(Helper.GetConnetction());
            _context=new PeopleContext("PeopleContext");
        }

        public bool Create(PeopleBase t)
        {
            var result = false;
            try
            {
                _context.Peoples.Add(t);
                result = _context.SaveChanges() > 0;
            }
            catch (Exception e)
            {
                var ex = string.Format("Exception in work with base: {0}", e.Message);
                throw new DalException(ex);
            }
            return result;
        }

        public PeopleBase Read(Guid id)
        {
            PeopleBase people = null;
            try
            {
                people = _context.Peoples.FirstOrDefault(s => s.Id == id);
            }
            catch (Exception e)
            {
                var ex = string.Format("Exception in work with base: {0}", e.Message);
                throw new DalException(ex);
            }
            return people;
        }

        public List<PeopleBase> ReadAll()
        {
            List<PeopleBase> lst = null;
            try
            {
                lst=_context.Peoples.ToList();
            }
            catch (Exception e)
            {
                var ex = string.Format("Exception in work with base: {0}", e.Message);
                throw new DalException(ex);
            }
            return lst;
        }

        public List<PeopleBase> ReadFromAge(int minAge, int maxAge)
        {
            List<PeopleBase> lst = null;
            try
            {
                var peoples = _context.Peoples.ToList();
                lst= peoples.Where(people => people.BirthDate.Year >= minAge).ToList();
            }
            catch(Exception e)
            {
                var ex = string.Format("Exception in work with base: {0}", e.Message);
                throw new DalException(ex);
            }
            return lst;
        }
    }


}
